package ma.ensias.healthme.gerant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import ma.ensias.healthme.R;
import ma.ensias.healthme.dao.MaladieDao;
import ma.ensias.healthme.dao.SymptomeDao;
import ma.ensias.healthme.entities.Maladie;
import ma.ensias.healthme.entities.Symptome;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class ListMaladiesActivity extends Activity {

	ExpandableListAdapter listAdapter;
	ExpandableListView expListView;
	List<Maladie> listDataHeader;
	HashMap<Integer, List<Symptome>> listDataChild=new HashMap<Integer, List<Symptome>>();
	MaladieDao malDao = new MaladieDao();
	SymptomeDao symDao = new SymptomeDao();
	final Context context = this;
	List<Maladie> listMal=new ArrayList<Maladie>();
	int idGroup;
	int idChild;
	HashMap<Integer, Integer> listAssocGrp=new HashMap<Integer, Integer>();
	HashMap<Integer, HashMap<Integer, Integer>> listAssoChd=new HashMap<Integer, HashMap<Integer,Integer>>();
	int previousItem = -1;
	String child="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_mala);

		// get the listview
		expListView = (ExpandableListView) findViewById(R.id.lvExp);

		// preparing list data
		prepareListData();

		listAdapter = new ExpandableListAdapter(this, listDataHeader,
				listDataChild);

		// setting list adapter
		expListView.setAdapter(listAdapter);

		// Listview Group click listener
		expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				// myGroup = (String) listAdapter.getGroup(groupPosition);
				expListView.smoothScrollToPosition(groupPosition);
				if (groupPosition != previousItem)
					expListView.collapseGroup(previousItem);
				previousItem = groupPosition;
			}
		});

		// Listview on child click listener
		expListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				idGroup = groupPosition;
				idChild = childPosition;
				System.out.println("Click Child");
				child="yes";
				return false;
			}
		});

		
		// Listview on child click listener
		expListView.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				// TODO Auto-generated method stub
				idGroup = groupPosition;
				System.out.println("Click Groupe");
				child="no";
				System.out.println(listDataHeader.get(idGroup).getNom());
				return false;
			}
		});
		
		
		
		
		registerForContextMenu(expListView);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, v.getId(), 0, "Modifier");
		menu.add(0, v.getId(), 0, "Supprimer");
	
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		try {

			if (item.getTitle() == "Modifier") {

			} else if (item.getTitle() == "Supprimer") {
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						context);

				// set title
				alertDialogBuilder.setTitle("Confirmation");

				// set dialog message
				alertDialogBuilder
						.setMessage("Voulez-vous supprimer?")
						.setCancelable(false)
						.setPositiveButton("Yes",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										Object item;
										/*if(child=="yes"){
											item=((ExpandableListAdapter)expListView.getAdapter()).getChild(idGroup, idChild);
											System.out.println("CHild");
										}
										else
										{
											item=((ExpandableListAdapter)expListView.getAdapter()).getGroup(idGroup);
											System.out.println("Group");
										}*/
										
										System.out.println(listDataHeader.get(idGroup).getNom());
										
										//malDao.delete(id)
										
										/*
										int idMal = listAssocGrp.get(idGroup);
										if (listAssocGrp.containsKey(idGroup)) {
											malDao.delete(idMal);
											listDataHeader.remove(idGroup);

										} else {
											HashMap<Integer, Integer> SymValue = listAssoChd
													.get(idGroup);
											int idSym = SymValue.get(idChild);
											Maladie maladie = malDao
													.getMaladie(idGroup);
											maladie.getSymptomes().remove(
													symDao.getSymptome(idSym));
											malDao.update(maladie);

											List<String> listchild = listDataChild
													.get(idGroup);
											listchild.remove(idChild);
											listDataChild.remove(idGroup);
											listDataChild.put(
													listDataHeader.get(idGroup),
													listchild);
										}
										listAdapter = new ExpandableListAdapter(
												getBaseContext(),
												listDataHeader, listDataChild);
										// setting list adapter
										expListView.setAdapter(listAdapter);*/
									}
								})
						.setNegativeButton("No",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										dialog.cancel();
									}
								});

				// create alert dialog
				AlertDialog alertDialog = alertDialogBuilder.create();

				// show it
				alertDialog.show();
			} else {
				return false;
			}
			return true;
		} catch (Exception e) {
			return true;
		}
	}

	/*
	 * Preparing the list data
	 */
	private void prepareListData() {

		listMal = malDao.getMaladies();
		//Maladie m=new Maladie("Mal1", "Desc ");
		listDataHeader = new ArrayList<Maladie>();
		listDataChild = new HashMap<Integer, List<Symptome>>();
		//HashMap<Integer, Integer> symptAsso=new HashMap<Integer, Integer>();
		int i = 0;
		for (Maladie maladie : listMal) {
			listDataHeader.add(maladie);
			//listAssocGrp.put(maladie.getId(), maladie.getId());
			//malDao.loadSymptomes(maladie);
			//symptAsso = new HashMap<Integer, Integer>();
			List<String> listSym = new ArrayList<String>();
			
			MaladieDao mdao=new MaladieDao();
			List<Symptome> listeSy=new ArrayList<Symptome>();
			List<Integer> Ids=mdao.getIdsSymptomes(maladie.getId());
			SymptomeDao sdao=new SymptomeDao();
			
			for(int id:Ids){
				listeSy.add(sdao.getSymptome(id));
			}
			
			
			
			for (Symptome symptome : listeSy) {
				
				listSym.add(symptome.getNom());
				//symptAsso.put(listDataChild.get(i).size() - 1, symptome.getId());
			}
			//listAssoChd.put((listDataHeader.size() - 1), symptAsso);
			listDataChild.put(maladie.getId(), listeSy);
			i++;
		}

	}
}
